

package com.hazelcast.cp.exception;

import com.hazelcast.core.IndeterminateOperationState;
import com.hazelcast.cp.internal.raft.impl.RaftEndpoint;

import java.io.Serial;
import java.util.UUID;

/**
 * A {@code CPSubsystemException} which is thrown when a Raft leader node
 * appends an entry to its local Raft log, but demotes to the follower role
 * before learning the commit status of the entry. In this case, this node
 * cannot decide if the operation is committed or not.
 */
public class StaleAppendRequestException extends CPSubsystemException implements IndeterminateOperationState {

    @Serial
    private static final long serialVersionUID = -736303015926722821L;

    public StaleAppendRequestException(RaftEndpoint leader) {
        super(leader != null ? leader.getUuid() : null);
    }

    private StaleAppendRequestException(UUID leaderUuid, Throwable cause) {
        super(null, cause, leaderUuid);
    }

    @Override
    public StaleAppendRequestException wrap() {
        return new StaleAppendRequestException(getLeaderUuid(), this);
    }
}
